import { login, logout, getInfo, feishuLogin } from '@/api/login'
import { getToken, setToken, removeToken,setCookie } from '@/utils/auth'
import defAva from '@/assets/images/profile.jpg'

const useUserStore = defineStore(
  'user',
  {
    state: () => ({
      token: getToken(),
      name: '',
      realname:'',
      avatar: '',
      roles: [],
      permissions: [],
      user_id: '',
      pwd_has_overdue:'',
      login_type:'',
      up_pwd_time:'',
      pwd_overdue_tip:''
    }),
    actions: {
      qrLogim(data) {
        return new Promise((resolve, reject) => {
          feishuLogin(data).then(res => {
            setToken(res.data.access_token)
            this.token = res.data.access_token
            setCookie('isLogin',1)
            resolve()
          }).catch(error => {
            reject(error)
          })

        })
      },
      // 登录
      login(userInfo) {
        const account = userInfo.account.trim()
        const password = userInfo.password
        const tenant_id = userInfo.tenant_id
        const uuid = userInfo.uuid
        return new Promise((resolve, reject) => {
          login(account, password, tenant_id, uuid).then(res => {
            setToken(res.data.access_token)
            this.token = res.data.access_token
            setCookie('isLogin',1)
            localStorage.setItem('currentUser', JSON.stringify(this.token))
            resolve()
          }).catch(error => {
            reject(error)
          })
        })
      },
      // 获取用户信息
      getInfo() {
        return new Promise((resolve, reject) => {
          getInfo().then(res => {
            const user = res.data.user
            const avatar = (user.picture == "" || user.picture == null) ? defAva : user.picture;
            if (res.roles && res.roles.length > 0) { // 验证返回的roles是否是一个非空数组
              this.roles = res.data.roles
              this.permissions = res.data.name
            } else {
              this.roles = ['ROLE_DEFAULT']
            }
            this.departments = res.data.departments
            this.pwd_has_overdue = res.data.pwd_has_overdue
            this.login_type = res.data.login_type
            this.up_pwd_time = res.data.up_pwd_time
            this.pwd_overdue_tip = res.data.pwd_overdue_tip
            window.userInfo = res.data
            this.name = user.en_name
            this.realname = user.realname
            this.avatar = avatar;
            this.user_id = user.user_id
            resolve(res)
          }).catch(error => {
            this.roles = ['ROLE_DEFAULT']
            reject(error)
          })
        })
      },
      // 退出系统
      logOut() {
        return new Promise((resolve, reject) => {
          const cleanToken = () => {
            this.token = ''
            this.roles = []
            this.permissions = []
            removeToken()
            resolve()
          }
          logout(this.token).then(() => {
            cleanToken()
            sessionStorage.clear()
            resolve()
          }).catch(error => {
            cleanToken()
            reject(error)
          })
        })
      },
      // 退出登陆状态  测试模块
      islogout(){
        logout(this.token)
      }
      
    }
  })

export default useUserStore
